package com.example.messagedemo.consumer;

import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MessageConsumer implements MessageListenerConcurrently {
    Logger logger = LoggerFactory.getLogger(MessageConsumer.class);

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                                                    ConsumeConcurrentlyContext context) {
        for (MessageExt message : msgs) {

            if(message.getTraceId()!=null) {
                logger.info("消费主题：{},消费内容：{}，消息ID: {}, 消息轨迹ID: {}, 轨迹进程ID: {}, 父消息ID: {}, 级别：{}", message.getBusinessTopic(),
                        new String(message.getBody()), message.getKeys(), message.getTraceId(), message.getProcessId(), message.getParentId(), message.getRank());
            }else {
                logger.info("消费主题：{},消费内容：{}，消息ID: {}, 级别：{}", message.getBusinessTopic(),
                        new String(message.getBody()), message.getKeys(), message.getRank()==null?"":message.getRank());
            }

        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}